package com.rizzo.analytics.services.web.controller.v1;

import com.rizzo.analytics.domain.account.Account;
import com.rizzo.analytics.domain.notification.Notifications;
import com.rizzo.infrastructure.dark.DarkLaunch;
import com.rizzo.infrastructure.performance.Monitor;
import com.rizzo.infrastructure.quota.Quota;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller("notificationController")
@RequestMapping(value = "/notification", produces = "application/json")
@Api(value = "/notification", description = "Operations about Notifications")
public class NotificationController extends RizzoController {

	private static final Logger LOGGER = LoggerFactory.getLogger(NotificationController.class);

	@Monitor(userBound = true)
	@PreAuthorize("hasRole('" + Account.ROLE_USER + "')")
	@Quota(maxCalls = 60, expireSeconds = 120)
	@DarkLaunch(name = "get-notifications")
	@RequestMapping(value = "/{application}", method = RequestMethod.GET, produces = "application/json")
	@ApiOperation(value = "Lists the notification data", tags = "notification",
			notes = "This endpoint returns all the notifications for a given application. " +
					"This endpoint can only be called when logged in!",
			responseClass = "com.rizzo.analytics.domain.notification.Notifications")
	public Notifications getNotifications(
			@PathVariable(value = "application")
			@ApiParam(name = "application", required = true,
					value = "The full name of the application " +
							"(see output of '/application' for possible values)") String application) {
		final String user = getUsernameFromContext();
		LOGGER.debug("Called notifications for [application : '{}'] and [user : '{}']", application, user);
		return analyticsDelegator.getNotifications(user, application, WILDCARD, Long.parseLong(ID_WILDCARD));
	}

	@Monitor(userBound = true)
	@PreAuthorize("hasRole('" + Account.ROLE_USER + "')")
	@Quota(maxCalls = 60, expireSeconds = 120)
	@DarkLaunch(name = "get-notifications-with-criteria")
	@RequestMapping(value = "/{application}/{category}", method = RequestMethod.GET, produces = "application/json", params = {"notificationId"})
	@ApiOperation(value = "Lists the filterable notification data", tags = "notification",
			notes = "This endpoint returns all the notifications for a given application. " +
					"The results of this endpoint are filterable by category and notificationId. " +
					"This endpoint can only be called when logged in!",
			responseClass = "com.rizzo.analytics.domain.notification.Notifications")
	public Notifications getNotificationsWithCriteria(
			@PathVariable(value = "application")
			@ApiParam(name = "application", required = true,
					value = "The full name of the application " +
							"(see output of '/application' for possible values)") String application,
			@PathVariable("category")
			@ApiParam(name = "category", required = true,
					value = "The category or method of the notified service (wildcards '*' are allowed but case-sensitive)") String category,
			@RequestParam(value = "notificationId", defaultValue = ID_WILDCARD, required = false)
			@ApiParam(name = "notificationId", defaultValue = ID_WILDCARD, required = false,
					value = "The unique ID of the notification (-1 means 'all ids')") long notificationId) {
		final String user = getUsernameFromContext();
		LOGGER.debug("Called notifications for [application : '{}'] and [user : '{}'] with [category : '{}']", application, user, category);
		return analyticsDelegator.getNotifications(user, application, category, notificationId);
	}

}
